package cn.interceptor;

import cn.entity.SysResource;
import cn.entity.SysUser;
import cn.service.SysResourceService;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class ResourceInterceptor implements HandlerInterceptor {

    private SysResourceService sysResourceService;

    public ResourceInterceptor(SysResourceService sysResourceService) {
        this.sysResourceService = sysResourceService;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        List<SysResource> sysResources = sysResourceService.selectResources();
        String uri = request.getRequestURI();
        SysUser user = (SysUser) request.getSession().getAttribute("user");

        if (uri.startsWith("/guguanjia/common/")||uri.startsWith("/guguanjia/resource/initMenu")){
            return true;
        }
        uri=uri.replace("/guguanjia/","");
            List<String> urls = user.getUrl();
            for (String url : urls) {
                if (url.equals("")){
                    continue;
                }
                url=url.substring(url.indexOf("/")+1)+"/";
                if ((uri+"/").startsWith(url)){
//                    System.out.println(uri);
//                    System.out.println(url);
                    return true;
                }
            }
            return true;

    }
}
